import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		/**
		 * 是否需要强制登录
		 */
		forcedLogin: false,
		hasLogin: false,
		userName: "",
		baseUrl: "",
		token: "",
		uid: "",
		is_manager: false
	},
	mutations: {
		login(state, userName) {
			state.userName = userName || '新用户';
			state.hasLogin = true;
		},
		logout(state) {
			uni.showModal({
				title: '提示',
				content: '是否要退出登录 ？',
				success: function(res) {
					if (res.confirm) {
						state.userName = "";
						state.hasLogin = false;
						state.uid = ""
						state.token = ""
						uni.reLaunch({
							url: '/pages/login/login'
						});
					} else if (res.cancel) {
						console.log('用户点击取消');
					}
				}
			});
		},
		setLoginState(state, obj) {
			state.hasLogin = obj.hasLogin;
			state.token = obj.token;
			state.uid = obj.uid;
			state.is_manager = obj.is_manager
			state.userName = obj.userName
		},
		setBaseUrl(state, url) {
			state.baseUrl = url
		}
	}
})

export default store
